package com.star.uc.factory.login.impl;

import com.alibaba.fastjson2.JSON;
import com.star.common.core.contract.exception.ServiceException;
import com.star.common.core.operator.OperatorRouter;
import com.star.uc.contract.dto.request.LoginRequest;
import com.star.uc.contract.enums.LoginTypeEnum;
import com.star.uc.contract.enums.UserErrorEnum;
import com.star.uc.contract.param.UserInfoParam;
import com.star.uc.contract.param.UserOuterMapperInfoParam;
import com.star.uc.factory.login.AbstractLoginTypeOperator;
import com.star.uc.infras.repository.model.UserInfoBO;
import com.star.uc.infras.repository.model.UserOuterMapperInfoBO;
import com.star.uc.infras.utils.FormValidatorUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 用户名密码登录
 *
 * @author zhouhengzhe
 * @version 1.0
 * @date 2023/10/19 17:31
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class UsernameLoginTypeOperator extends AbstractLoginTypeOperator {

    /**
     * 检查对应的登录类型的参数
     *
     * @param loginRequest 登录请求参数
     */
    @Override
    public void checkParam(LoginRequest loginRequest) {
        LoginRequest.UsernameRequest usernameRequest = loginRequest.getUsernameRequest();
        if (Objects.isNull(usernameRequest)
                || StringUtils.isBlank(usernameRequest.getUsername())
                || StringUtils.isBlank(usernameRequest.getPassword())) {
            log.error("用户名密码登录失败，参数为:{}", JSON.toJSONString(usernameRequest));
            throw new ServiceException(UserErrorEnum.USERNAME_OR_PASSWORD_NULL);
        }
    }

    /**
     * 登录
     *
     * @param loginRequest 登录请求
     * @return 用户信息
     */
    @Override
    public UserInfoBO authorize(LoginRequest loginRequest) {
        //检验参数
        checkParam(loginRequest);
        LoginRequest.UsernameRequest usernameRequest = loginRequest.getUsernameRequest();
        //判断当前用户是否包含是邮箱
        UserInfoBO userInfoBO = null;
        if (FormValidatorUtils.validateEmail(usernameRequest.getUsername())) {
            //查询email映射信息
            UserOuterMapperInfoBO userOuterMapperInfoBO = UserOuterMapperInfoParam
                    .builder()
                    .tenantId(loginRequest.getTenantId())
                    .identifier(usernameRequest.getUsername())
                    .loginType(LoginTypeEnum.EMAIL.getCode())
                    .build().one();
            //查到就查一下数据信息
            if (Objects.nonNull(userOuterMapperInfoBO)) {
                userInfoBO = UserInfoParam
                        .builder()
                        .uid(userOuterMapperInfoBO.getUid())
                        .build().one();
            }
            //国际国外电话号码校验，防止非手机号码乱搞
        } else if (FormValidatorUtils.validatePhone(usernameRequest.getUsername())) {
            String[] phones = usernameRequest.getUsername().split(",");
            //查询email映射信息
            UserOuterMapperInfoBO userOuterMapperInfoBO = UserOuterMapperInfoParam
                    .builder()
                    .tenantId(loginRequest.getTenantId())
                    .identifier(phones[1])
                    .countryCode(phones[0])
                    .loginType(LoginTypeEnum.PHONE.getCode())
                    .build().one();
            //查到就查一下数据信息
            if (Objects.nonNull(userOuterMapperInfoBO)) {
                userInfoBO = UserInfoParam
                        .builder()
                        .uid(userOuterMapperInfoBO.getUid())
                        .build().one();
            }
        }
        //检查用户是否存在或者禁用
        return checkUserInfoExistOrDisable(userInfoBO, loginRequest);
    }

    /**
     * Operator的名称, 同一类型的Operator的路由器{@link OperatorRouter}能够根据该值路由到当前的Operator
     *
     * @return route key
     */
    @Override
    public String getName() {
        return LoginTypeEnum.USERNAME.getCode();
    }

}
